package com.google.code

import scalax.io.{Codec, Resource, Output}
import java.io.File

object XmlDocument {
  def apply(entry: Option[XmlNode], head: Option[Head], file: File) =
    new XmlDocument(entry, head, file)

  def empty(file: File) = new XmlDocument(None, None, file)
}

class XmlDocument(val entry: Option[XmlNode], hd: Option[Head], file: File) {
  implicit private val defaultCodec: Codec = Codec.UTF8

  val head = hd getOrElse Head("1.0", "utf-8")

  def save(): Unit = {
    val out: Output = Resource.fromFile(file)

    out write this.toString
  }

  def changeEntry(newEntry: Option[XmlNode]) = XmlDocument(newEntry, hd, file)
  def changeHead(newHead: Option[Head]) = XmlDocument(entry, newHead, file)

  override def toString: String = {
    val h = head
    val e = entry getOrElse ""
    h + "\n" + e
  }
}
